package 变长滑动窗口1

func minOperations(nums []int, x int) int {
	left := 0
	s := 0
	s2 := 0
	maxL := -1
	for _, num := range nums {
		s += num
	}
	target := s - x
	if target == 0 {
		return len(nums)
	}
	if s < x {
		return -1
	}
	for i, num := range nums {
		s2 += num
		for s2 > s-x && left <= i {
			leftN := nums[left]
			s2 -= leftN
			left++
		}
		if s2 == target {
			maxL = max(i-left+1, maxL)
		}
	}
	if maxL != -1 {
		return len(nums) - maxL
	}
	return -1
}
